<?php

// [ 框架函数库 ]

if ( !function_exists('p') ) {
    /**
     * [p打印]
     * @param  [type] $val [description]
     * @return [type]      [description]
     */
    function p($val)
    {
        header('Content-Type: text/html; charset=utf-8');
        echo '<div style="margin: 20px; padding: 20px; background-color: #5A5A5A; color: #ffffff;">';
        if ( true === $val || false === $val ) {
            var_export($val);
        } elseif ( is_array($val) || is_object($val) ) {
            echo '<pre>';
            print_r($val);
            echo '</pre>';
        } else {
            printf($val);
        }
        echo '</div>';
    }
}

if ( !function_exists('allow_cross_request') )
{
    /**
     * [api允许跨域请求]
     * @return [type] [description]
     */
    function allow_cross_request()
    {
        header("Access-Control-Allow-Origin:*");
        header("Access-Control-Allow-Methods:GET,POST,PUT,DELETE");
    }
}

if ( !function_exists('get_client_ip') )
{
    /**
     * [获取客户端ip]
     * @return [type] [description]
     */
    function get_client_ip()
    {
        if ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos = array_search('unknown', $arr);
            if (false !== $pos) unset($arr[$pos]);
            $ip = trim( current($arr) );
        } elseif ( isset($_SERVER['HTTP_CLIENT_IP']) ) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif ( isset($_SERVER['REMOTE_ADDR']) ) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
}
